package com.by.controller;

import com.by.bean.Category;
import com.by.common.bean.ResultBean;
import com.by.common.controller.BaseController;
import com.by.common.group.ISave;
import com.by.common.group.IUpdate;
import com.by.query.CategoryQuery;
import com.by.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Ye QS
 * @version 0.1
 * @className CategoryController
 * @description
 * @date 2024/10/7 17:25
 * @since jdk11
 */
@RestController
@RequestMapping("/v1")
@Api(tags = "产品分类管理")
public class CategoryController extends BaseController {

    @Autowired
    CategoryService categoryService;



    @ApiOperation("保存产品分类")
    @PostMapping("/category/save")
    public ResultBean save(@Validated(ISave.class) @RequestBody Category category, BindingResult result) {
        validate(result);
        int res = categoryService.save(category);
        return saveResult(res);
    }

    @ApiOperation("删除产品分类")
    @DeleteMapping("/category/delete")
    public ResultBean delete(@RequestParam(name = "id", required = true, defaultValue = "0") int id) {
        int res = categoryService.delete(id);
        return deleteResult(res);
    }

    @ApiOperation("修改产品分类")
    @PutMapping("/category/update")
    public ResultBean update(@Validated(IUpdate.class) @RequestBody Category category, BindingResult result) {
        validate(result);
        int res = categoryService.update(category);
        return updateResult(res);
    }

    @ApiOperation("产品分类列表")
    @PostMapping("/category/queryAll")
    public ResultBean queryAll(@RequestBody CategoryQuery categoryQuery) {
        List list = categoryService.queryAll(categoryQuery);
        return success("产品分类列表", list);
    }


}